package cn.mumzone.pos.presenter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunmi.payment.PaymentService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.basic.util.log.Elog;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.BuildConfig;
import cn.mumzone.pos.model.ReturnEntity;
import cn.mumzone.pos.model.ReturnGoodsEntity;
import cn.mumzone.pos.model.source.GoodsRepository;
import cn.mumzone.pos.model.SunMiPayRequestEntity;
import cn.mumzone.pos.model.VerifyReturnAuthEntity;
import cn.mumzone.pos.model.source.AppRepository;
import cn.mumzone.pos.model.source.UserRepository;
import cn.mumzone.pos.presenter.contract.BillReturnContract;
import cn.mumzone.pos.util.BillUtil;
import cn.mumzone.pos.util.PayCountDownTimer;

/**
 * Created by JoeLjt on 2019/7/9.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class BillReturnNewPresenter extends AbsPresenter<BillReturnContract.View, GoodsRepository> {


    public static final int CODE_INVENTORY_ERROR = 507;

    UserRepository userRepository;


    @Override
    protected void initOtherRepository() {
        userRepository = new UserRepository();
    }

    public void initEvent() {
    }

    public void searchGoodsForReturn(String orderCode, String goodsCode) {
        ApiCallBack<List<ReturnGoodsEntity>> callBack = new ApiCallBack<List<ReturnGoodsEntity>>() {
            @Override
            public void onSuccess(List<ReturnGoodsEntity> returnGoodsEntities) {
                mView.showReturnGoodsList(returnGoodsEntities);
            }

            @Override
            public void onError(BaseResponse response) {
                mView.showError(response.getMsg());
            }
        };
        mRepo.searchGoodsReturnList(orderCode, goodsCode, callBack);
        addSubscrebe(callBack);
    }

    public void verifyReturnAuth() {
        mView.showLoadingDialog();
        ApiCallBack<VerifyReturnAuthEntity> callBack = new ApiCallBack<VerifyReturnAuthEntity>() {
            @Override
            public void onSuccess(VerifyReturnAuthEntity verifyReturnAuthEntity) {
                mView.dismissLoadingDialog();
                if (!StringUtils.isEmpty(verifyReturnAuthEntity.getAccessType()) && verifyReturnAuthEntity.getAccessType().equals("1")) {
                    mView.showUpdateReturnAmount();
                } else {
                    mView.goAuthView();
                }
            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showError(response.getMsg());
            }
        };
        userRepository.verifyReturnAuth(AppConstants.TEMP_SELLER_ID, "", "", callBack);
        addSubscrebe(callBack);
    }

    public void confirmForReturn(final String goodsList, final String orderCode, final String returnAmount, final String returnMargin) {

        ApiCallBack<List<ReturnEntity>> callBack = new ApiCallBack<List<ReturnEntity>>() {
            @Override
            public void onSuccess(List<ReturnEntity> billReturnEntities) {
                mView.showReturnSuccess(billReturnEntities);
            }

            @Override
            public void onError(BaseResponse response) {
                if(response.getCode() == CODE_INVENTORY_ERROR){
                    mView.showInventoryError(response.getMsg());
                    return;
                }
                mView.showReturnFailed(response.getMsg());
            }
        };

        mRepo.confirmForReturn(goodsList, orderCode, returnAmount, returnMargin, callBack);
        addSubscrebe(callBack);
    }


    @Override
    public void detachView() {
        super.detachView();
    }
}
